package com.paic.mhis.hcpms.trust.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.common.util.HttpProcessUtil;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.trust.biz.service.InsuredMedicalApproveService;
import com.paic.mhis.hcpms.trust.util.TrustConstants;
import com.paic.mhis.hcpms.trust.web.vo.DoctorIntegrityVO;
import com.paic.mhis.hcpms.trust.web.vo.IntegrityMedicalVO;
import com.paic.mhis.hcpms.trust.web.vo.TrustUtil;
import com.paic.pafa.app.biz.service.BusinessServiceException;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/integrityApprove/")
public class InsuredMedicalApproveController {
	@BizServicesProxy(beanName = "insuredMedicalApproveService")
	private InsuredMedicalApproveService insuredMedicalApproveService;

	
	/**
	 * 医护人员审批查询列表
	 * 
	 * @param limit
	 * @param start
	 * @param search
	 * @param request
	 * @return 返回查询的集合和总条数
	 * @throws BusinessServiceException
	 * @throws BusinessException
	 */
	@RequestMapping("integrityApproveList.do")
	@ResponseBody
	public Map<String, Object> medicalDetailList(@ModelAttribute IntegrityMedicalVO search,
			@RequestParam(value = "limit", defaultValue = "20") int limit,
			@RequestParam(value = "start", defaultValue = "0") int start, HttpServletRequest request)
			throws BusinessServiceException, BusinessException {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		paramMap.put("limit", limit);
		paramMap.put("start", start);

		List<String> statusList = new ArrayList<String>();
		statusList.add(TrustConstants.STATUS_WAIT_APPROVE);
		paramMap.put("userName", user.getUsername());
		paramMap.put("userRole", TrustConstants.INTEGRITY_APPROVER_ROLE);
		paramMap.put("statusList", statusList);
		paramMap.put("nextStatus", TrustConstants.STATUS_CLOSED);

		TrustUtil.paramMap(paramMap, search);
		try {
			result = insuredMedicalApproveService.getMedicalApproveList(paramMap);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
	/**
	 * 医护人员审批详情
	 * @return
	 */
	@RequestMapping("integrityApproveDetail.do")
	@ResponseBody
	public Map<String, Object> integrityApproveDetail(@ModelAttribute DoctorIntegrityVO search,
			HttpServletRequest request){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("targetType",TrustConstants.TARGETTYPE_TWO);//设置扣分对象类型为医生
		paramMap.put("targetNo", search.getDoctorCode());
		paramMap.put("orgCode", search.getOrgCode());
		paramMap.put("caseType", TrustConstants.CASE_TYPE);//代表医务人员记分录入
		paramMap.put("caseNo", search.getTaskNo());
		String status = search.getStatus();
		paramMap.put("status", status);
		paramMap.put("taskNo", search.getTaskNo());
		Map<String, Object> result = new HashMap<String, Object>();
		//根据参数查询审批对象
		result=insuredMedicalApproveService.findByCaseNo(paramMap);
		//taskno案件(任务)编号
		String insuranceName=(String) result.get("insuranceName");
		System.out.println(insuranceName);
		return result;
	}
	
	/**
	 * 组装参数
	 * 
	 * @param paramMap
	 * @param search
	 */
	private void paramMap(Map<Object, Object> paramMap, IntegrityMedicalVO search) {
		paramMap.put("startDate", StringUtils.isEmpty(search.getStartDate()) ? null : search.getStartDate());
		paramMap.put("endDate", StringUtils.isEmpty(search.getEndDate()) ? null : search.getEndDate());
		if (StringUtils.isNotEmpty(search.getDoctorCode())) {
			String[] paramStr = search.getDoctorCode().split("\\；\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("doctorCode", paramStr);
		} else {
			paramMap.put("doctorCode", null);
		}
		if (StringUtils.isNotEmpty(search.getOrgCode())) {
			String[] paramStr = search.getOrgCode().split("\\；\\s*|;\\s*|,\\s*|，\\s*");
			paramMap.put("orgCode", paramStr);
		} else {
			paramMap.put("orgCode", null);
		}
	}

}
